package com.kly.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kly.springbootinit.common.ErrorCode;
import com.kly.springbootinit.constant.CommonConstant;
import com.kly.springbootinit.exception.ThrowUtils;
import com.kly.springbootinit.mapper.VehicleMapper;
import com.kly.springbootinit.model.dto.vehicle.VehicleQueryRequest;
import com.kly.springbootinit.model.dto.vehicle.VehicleRentRequest;
import com.kly.springbootinit.model.entity.Vehicle;
import com.kly.springbootinit.model.vo.VehicleVO;
import com.kly.springbootinit.service.OrderService;
import com.kly.springbootinit.service.UserService;
import com.kly.springbootinit.service.VehicleService;
import com.kly.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 车辆管理服务实现
 *
 * @author <a href="https://github.com/likly">程序员鹿七</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Service
@Slf4j
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {

    @Resource
    private UserService userService;

    @Resource
    private OrderService orderService;

    /**
     * 校验数据
     *
     * @param vehicle
     * @param add     对创建的数据进行校验
     */
    @Override
    public void validVehicle(Vehicle vehicle, boolean add) {
        ThrowUtils.throwIf(vehicle == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        String title = vehicle.getModel();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
        if (StringUtils.isNotBlank(title)) {
            ThrowUtils.throwIf(title.length() > 80, ErrorCode.PARAMS_ERROR, "标题过长");
        }
    }

    /**
     * 获取查询条件
     *
     * @param vehicleQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Vehicle> getQueryWrapper(VehicleQueryRequest vehicleQueryRequest) {
        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        if (vehicleQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        String model = vehicleQueryRequest.getModel();
        String licensePlate = vehicleQueryRequest.getLicensePlate();
        Integer status = vehicleQueryRequest.getStatus();
        BigDecimal dailyRate = vehicleQueryRequest.getDailyRate();
        String sortField = vehicleQueryRequest.getSortField();
        String sortOrder = vehicleQueryRequest.getSortOrder();

        // todo 补充需要的查询条件
        // 从多字段中搜索
        queryWrapper.like(StrUtil.isNotBlank(licensePlate), "license_plate", licensePlate);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq(dailyRate != null, "daily_rate", dailyRate);
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(model), "model", model);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取车辆管理封装
     *
     * @param vehicle
     * @param request
     * @return
     */
    @Override
    public VehicleVO getVehicleVO(Vehicle vehicle, HttpServletRequest request) {
        // 对象转封装类
        VehicleVO vehicleVO = VehicleVO.objToVo(vehicle);
        return vehicleVO;
    }

    /**
     * 分页获取车辆管理封装
     *
     * @param vehiclePage
     * @param request
     * @return
     */
    @Override
    public Page<VehicleVO> getVehicleVOPage(Page<Vehicle> vehiclePage, HttpServletRequest request) {
        List<Vehicle> vehicleList = vehiclePage.getRecords();
        Page<VehicleVO> vehicleVOPage = new Page<>(vehiclePage.getCurrent(), vehiclePage.getSize(), vehiclePage.getTotal());
        if (CollUtil.isEmpty(vehicleList)) {
            return vehicleVOPage;
        }
        // 对象列表 => 封装对象列表
        List<VehicleVO> vehicleVOList = vehicleList.stream().map(vehicle -> {
            return VehicleVO.objToVo(vehicle);
        }).collect(Collectors.toList());


        vehicleVOPage.setRecords(vehicleVOList);
        return vehicleVOPage;
    }


}
